/*
 * items.h
 *
 *  Created on: 2011-11-14
 *      Author: shenjiong
 */

#ifndef ITEMS_H_
#define ITEMS_H_

#include <linux.h> 


enum protocol {
    ascii_prot = 3, /* arbitrary value. */
    binary_prot,
    negotiating_prot /* Discovering the protocol */
};

typedef struct {
    uint32_t evicted;
    uint32_t evicted_nonzero;
    rel_time_t evicted_time;
    uint32_t reclaimed;
    uint32_t outofmemory;
    uint32_t tailrepairs;
} itemstats_t;



typedef struct _stritem {
    struct _stritem *next;
    struct _stritem *prev;
    struct _stritem *h_next;    /* hash chain next */
    rel_time_t      time;       /* least recent access */
    rel_time_t      exptime;    /* expire time */
    int32_t             nbytes;     /* size of data */
    uint32_t            refcount;
    uint8_t         nsuffix;    /* length of flags-and-length string */
    uint8_t         it_flags;   /* ITEM_* above */
    uint8_t         slabs_clsid;/* which slab class we're in */
    uint8_t         nkey;       /* key length, w/terminating null and padding */
    void * end[];
    /* if it_flags & ITEM_CAS we have 8 bytes CAS */
    /* then null-terminated key */
    /* then " flags length\r\n" (no terminating null) */
    /* then data with terminating \r\n (no terminating null; it's binary!) */
} item;

struct  settings{
    uint32_t maxbytes;
    int32_t maxconns;
    int32_t port;
    int32_t udpport;
    char *inter;
    int32_t verbose;
    rel_time_t oldest_live; /* ignore existing items older than this */
    int32_t evict_to_free;
    char *socketpath;   /* path to unix socket if using local socket */
    int32_t access;  /* access mask (a la chmod) for unix domain socket */
    double factor;          /* chunk size growth factor */
    int32_t chunk_size;
    int32_t num_threads;        /* number of worker (without dispatcher) libevent threads to run */
    char prefix_delimiter;  /* character that marks a key prefix (for stats) */
    int32_t detail_enabled;     /* nonzero if we're collecting detailed stats */
    int32_t reqs_per_event;     /* Maximum number of io to process on each
                               io-event. */
    bool use_cas;
    enum protocol binding_protocol;
    int32_t backlog;
    int32_t item_size_max;        /* Maximum item size, and upper end for slabs */
    bool sasl;              /* SASL on/off */
} ;



/**
 * Callback for any function producing stats.
 *
 * @param key the stat's key
 * @param klen length of the key
 * @param val the stat's value in an ascii form (e.g. text form of a number)
 * @param vlen length of the value
 * @parm cookie magic callback cookie
 */
typedef void (*ADD_STAT)(const char *key, const uint16_t klen,
                         const char *val, const uint32_t vlen,
                         const void *cookie);


/* Forward Declarations */
static void item_link_q(item *it);
static void item_unlink_q(item *it);
void do_item_unlink(item *it);
#endif /* ITEMS_H_ */
